package com.swak.persistence.mybatis.methods.impls;

import com.swak.persistence.mybatis.methods.Sqls;
import com.swak.utils.StringUtils;

/**
 * 选择性的查询条件来删除记录，包括逻辑删除: 一般是唯一索引的使用方式
 * 
 * @author DELL
 */
public class DeleteSelective extends Delete {

	@Override
	protected void doInject() {
		if (!this.supportMethod(Sqls.DELETE_SELECTIVE)) {
			return;
		}
		StringBuilder sql = new StringBuilder();
		String logicSql = this.parseUpdateProperty();
		if (StringUtils.isNotBlank(logicSql)) {
			sql.append(SCRIPT_START).append(UPDATE).append(SPACE).append(this.table.getName()).append(SPACE).append(SET)
					.append(SPACE).append(logicSql).append(SPACE).append(this.parseSelectiveWhereEqualColumns())
					.append(SCRIPT_END).toString();
		} else {
			sql.append(SCRIPT_START).append(DELETE).append(SPACE).append(FROM).append(SPACE)
					.append(this.table.getName()).append(SPACE).append(SPACE)
					.append(this.parseSelectiveWhereEqualColumns()).append(SCRIPT_END).toString();
		}
		this.addDeleteMappedStatement(Sqls.DELETE_SELECTIVE.getMethod(), sql.toString());
	}
}
